from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.shortcuts import render, redirect

# Create your views here.
from django.template import loader
from django.urls import reverse

from app.models import User


def index(request):
    return HttpResponse('App2 Template')


def load_template(request):
    temp = loader.get_template('example.html') # 加载模版文件，生成模版对象
    print(temp, type(temp))
    print(temp.__dict__)
    res = temp.render(context={'content':'hello index'}) # 渲染结果
    print(res)
    return HttpResponse(res)

def filter_template(request):
    num = 10
    return render(request,'filter.html', context=locals())


def handle_ajax(request):
    if request.is_ajax():
        return JsonResponse({'msg':'登录成功'})
    return render(request,'index.html')


def register(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        print(username, password)
        return JsonResponse({'msg':'登录成功', 'username':username})
    return render(request, 'register.html')


def jinja(request):
    return render(request, 'index_jinja.html')
